﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Api.Common
{
    public class FileHelper
    {
        /// <summary>
        /// 刪除文件
        /// </summary>
        /// <param name="p">绝对路径或則相对路径</param>
        public static void Delete(params string[] paths)
        {
            foreach (string p in paths)
                try
                {
                    if (File.Exists(p))
                    {
                        File.Delete(p);
                    }
                    else
                    {
                        var path = HttpContext.Current.Server.MapPath(p);
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
                catch (Exception ex)
                {

                }
        }

        /// <summary>
        /// 判斷文件是否存在
        /// </summary>
        /// <param name="path">相对路径</param>
        /// <returns></returns>
        public static bool FileExist(string path)
        {
            if (File.Exists(path))
            {
                return true;
            }
            else if (File.Exists(HttpContext.Current.Server.MapPath(path)))
            {
                return true;
            }
            return false;
        }
    }
}
